define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales_return/index' + location.search,
                    add_url: 'sales_return/add',
                    edit_url: 'sales_return/edit',
                    del_url: 'sales_return/del',
                    multi_url: 'sales_return/multi',
                    import_url: 'sales_return/import',
                    table: 'sales_return',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'shipment_id', title: __('Shipment_id')},
                        {field: 'invoice_number', title: __('Invoice_number'), operate: 'LIKE'},
                        {field: 'return_reason', title: __('Return_reason'), searchList: {"质量问题":__('质量问题'),"发错货":__('发错货'),"客户取消":__('客户取消'),"运输损坏":__('运输损坏'),"其他":__('其他')}, formatter: Table.api.formatter.status},
                        {field: 'return_date', title: __('Return_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'refund_status', title: __('Refund_status'), searchList: {"未退款":__('未退款'),"已退款":__('已退款'),"部分退款":__('部分退款')}, formatter: Table.api.formatter.status},
                        {field: 'refund_amount', title: __('Refund_amount'), operate:'BETWEEN'},
                        {field: 'currency', title: __('Currency'), operate: 'LIKE'},
                        {field: 'tax_rate', title: __('Tax_rate'), operate: 'BETWEEN'},
                        {field: 'tax_amount', title: __('Tax_amount'), operate:'BETWEEN'},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'is_inventory_updated', title: __('Is_inventory_updated')},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                        {field: 'quantity', title: __('Quantity'), operate: '='},
                        {field: 'unit', title: __('Unit'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            Controller.api.initTaxCalculation();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.api.initTaxCalculation();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            initTaxCalculation: function () {
                // 税额和价格计算逻辑
                var calculateTaxAmount = function () {
                    var refundAmount = parseFloat($('#c-refund_amount').val()) || 0;
                    var taxRate = parseFloat($('#c-tax_rate').val()) || 0;

                    // 计算不含税价格：价格(不含税) = 含税金额 / (1 + 税率/100)
                    var priceWithoutTax = refundAmount / (1 + taxRate / 100);
                    // 计算税额：税额 = 含税金额 - 不含税价格
                    var taxAmount = refundAmount - priceWithoutTax;

                    // 设置计算结果，保留2位小数
                    $('#c-tax_amount').val(taxAmount.toFixed(2));
                    $('#c-total_amount').val(priceWithoutTax.toFixed(2));
                };

                // 绑定退款金额输入事件
                $('#c-refund_amount').on('input', calculateTaxAmount);

                // 绑定税率输入事件
                $('#c-tax_rate').on('input', calculateTaxAmount);

                // 页面加载时执行一次计算（用于编辑页面）
                calculateTaxAmount();
            }
        }
    };
    return Controller;
});
